//index.js
//获取应用实例
const app = getApp();
const global_url = app.globalData.url;

Page({
  data: {
    imgUrls: [],
    articleClassify: [],
    articleList: []
  },
  onLoad: function () {
    let that = this;

    console.log(that);

    // banner
    that.bannerListAction();

    // 文章分类
    that.articleClassifyAction();
  },
  // banner 列表
  bannerListAction: function() {
    let that = this;

    wx.request({
      url: global_url + 'banner/banner_list',
      method: 'GET',
      success: function(res) {
        if (res.data.errorCode == 0) {
          that.setData({
            imgUrls: res.data.data.info,
          })
        }
      }
    })
  },
  // 文章分类方法
  articleClassifyAction: function() {
    let that = this;

    wx.request({
      url: global_url + 'article/article_classify',
      method: 'GET',
      success: function (res) {
        if (res.data.errorCode == 0) {
          //  获取第一个分类id
          let first_classify_id = res.data.data.info == [] ? '' : res.data.data.info[0].id;

          that.setData({
            articleClassify: res.data.data.info
          })

          // 文章列表
          that.getArticleAction('', first_classify_id);
        }
      }
    })
  },
  // 点击获取文章列表
  getArticleAction: function(e, id) {
    let that = this, this_id = id != undefined ? id : e.currentTarget.dataset.id;

    app.loadingShow();    
    wx.request({
      url: global_url + 'article/article_list',
      method: 'GET',
      data: { classify_id: this_id },
      success: function (res) {
        if (res.data.errorCode == 0) {
          that.setData({
            classify_id: this_id,
            articleList: res.data.data.info,
          });

          app.loadingHide();
        }
      }
    })
  }
})
